package com.gorkr.offer.stackQueueHeap;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author gorkr
 * @date 2022/04/09 21:20
 * <p>
 * PriorityQueue 底层结构就是 小顶堆
 * TreeMap 底层时红黑树 用来排序 默认按照键升序
 **/
public class OfferEasy40 {

    public int[] getLeastNumbers(int[] arr, int k) {

        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int a : arr
        ) {
            queue.offer(a);
        }
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            result[i] = queue.poll();
        }
        return result;
    }

    @Test
    public void test() {
        int[] arr = {3, 2, 1};
        int k = 2;
        System.out.println(Arrays.toString(getLeastNumbers(arr, k)));
    }
}
    